package cn.liumouren.jzxd.controller;

import cn.liumouren.jzxd.common.utils.PageUtils;
import cn.liumouren.jzxd.common.utils.R;
import cn.liumouren.jzxd.entity.Zone;
import cn.liumouren.jzxd.service.ZoneService;
import cn.liumouren.jzxd.utils.QueryUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author Freeman Liu
 * @email llw599502537@gmail.com
 * @date 2020-10-31 00:44:45
 */
@RestController
@RequestMapping("/zone")
@Api(tags = "动态")
public class ZoneController {
    @Autowired
    private ZoneService zoneService;

    @ApiOperation("万能查询")
    @RequestMapping(value = "/customQuery", method = RequestMethod.GET)
    public R getSceneNames(@RequestParam Map<String, Object> map) {
        PageUtils result = QueryUtil.getResult(zoneService, map);
        return R.ok("查询成功", result);
    }

    /**
     * 列表
     */
    @ApiOperation("分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = zoneService.queryPage(params);

        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @ApiOperation("根据id查询")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public R info(@PathVariable("id") String id) {
        Zone zone = zoneService.getById(id);

        return R.ok().put("data", zone);
    }

    /**
     * 保存
     */
    @ApiOperation("新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public R save(@RequestBody Zone zone) {
        zoneService.save(zone);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public R update(@RequestBody Zone zone) {
        zoneService.updateById(zone);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("批量删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public R delete(@RequestBody String[] ids) {
        zoneService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @ApiOperation("分页查询某个用户的动态")
    @RequestMapping(value = "/getUserId/{userId}/{cur}/{size}", method = RequestMethod.GET)
    public R getUserId(@PathVariable String userId, @PathVariable int cur, @PathVariable int size) {

        IPage<Zone> zones = zoneService.findByUserId(userId, cur, size);
        return R.ok("成功查询用户[" + userId + "]第[" + cur + "]页(每页显示" + size + ")动态信息", zones);
    }

    @ApiOperation("分页查询用户点赞的动态")
    @RequestMapping(value = "/getLikedZonesByUserId/{userId}/{cur}/{size}", method = RequestMethod.GET)
    public R getLikedZonesByUserId(@PathVariable String userId, @PathVariable int cur, @PathVariable int size) {

        IPage<Zone> zones = zoneService.getLikedZonesByUserId(userId, cur, size);
        return R.ok("成功查询用户[" + userId + "]第[" + cur + "]页(每页显示" + size + ")点赞动态", zones);
    }

    @ApiOperation("分页查询用户评论的动态")
    @RequestMapping(value = "/getCommentedZonesByUserId/{userId}/{cur}/{size}", method = RequestMethod.GET)
    public R getCommentedZonesByUserId(@PathVariable String userId, @PathVariable int cur, @PathVariable int size) {

        IPage<Zone> zones = zoneService.getCommentedZonesByUserId(userId, cur, size);
        return R.ok("成功查询用户[" + userId + "]第[" + cur + "]页(每页显示" + size + ")评论动态", zones);
    }

    @ApiOperation("设置为私有动态(state为1设置为私密,0设置为公开)")
    @RequestMapping(value = "/makePrivacyOrPublic/{zoneId}/{state}", method = RequestMethod.PUT)
    public R makePrivacyOrPublic(@PathVariable String zoneId, @PathVariable int state) {

        zoneService.makePrivacyOrPublic(zoneId, state);
        return R.ok("成功设置动态[" + zoneId + "]为" + (state == 1 ? "'私密'" : "'公开'"));
    }

    @ApiOperation("分页查询动态数据")
    @RequestMapping(value = "/pageQuery", method = RequestMethod.GET)
    public R makePrivacyOrPublic(int cur, int size) {
        List<Zone> zones = zoneService.pageQuery(cur, size);
        return R.ok("成功查询第[" + cur + "]页(每页显示[" + size + "])动态", zones);
    }

    @ApiOperation("根据指定 column 分页升降序排序(0升序,1降序) [{'add_time':1},{'hot':0}]")
    @RequestMapping(value = "/pageQueryByColumn/{cur}/{size}", method = RequestMethod.POST)
    public R pageQueryByColumn(@RequestBody Map<String, Integer> map, @PathVariable int cur, @PathVariable int size) {
        // [{'add_time':1},{'hot':0}]
        IPage<Zone> zones = zoneService.pageQueryByColumn(map, cur, size);
        return R.ok("成功查询第[" + cur + "]页(每页显示[" + size + "])动态", zones);
    }

    @ApiOperation("分页获取相关的动态")
    @RequestMapping(value = "/getRelatedZones/{sceneId}/{cur}/{size}", method = RequestMethod.GET)
    public R getRelatedZones(@PathVariable String sceneId, @PathVariable int cur, @PathVariable int size) {
        List<Zone> zones = zoneService.getRelatedZones(sceneId, cur, size);
        return R.ok("成功获取与景点[" + sceneId + "]相关的第[" + cur + "]页(每页显示[" + size + "])动态", zones);
    }

}
